package com.lagou.edu.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

/**
 * @author 赵乾坤
 * @since 2021-12-04 14:10
 * 事务管理器类：负责手动事务的开启、提交、回滚
 * 基于面向对象思想，把方法封装为对象
 */
@Component("transactionManager")//bean标签注解
public class TransactionManager {

    @Autowired
    private ConnectionUtils connectionUtils;



    /**
     *  开启手动事务控制
     * @throws SQLException SQLException
     */
    public void beginTransaction() throws SQLException {
        // 关闭自动提交
        connectionUtils.getCurrentThreadConn().setAutoCommit(false);
    }

    /**
     * 提交事务
     * @throws SQLException SQLException
     */
    public void commit() throws SQLException {
        connectionUtils.getCurrentThreadConn().commit();
    }


    /**
     * 回滚事务
     * @throws SQLException SQLException
     */
    public void rollback() throws SQLException {
        connectionUtils.getCurrentThreadConn().rollback();
    }
}
